import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { WeappConfigService } from '../../services/admin/weapp-config.service';

/**
 * ConfigController
 * 对应 PHP: Config Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('weapp')
@Controller('adminapi/weapp')
export class ConfigController {
  constructor(private readonly weappConfigService: WeappConfigService) {}

  /**
   * 微信小程序配置
   * 路由: GET config
   * PHP路由: Route::get('config', 'weapp.Config/get')
   */
  @Get('config')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '微信小程序配置' })
  async get(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: get

      return await this.weappConfigService.getWeappConfig();
    } catch (error) {
      throw new BusinessException('get操作失败', error);
    }
  }

  /**
   * 微信小程序配置
   * 路由: PUT config
   * PHP路由: Route::put('config', 'weapp.Config/set')
   */
  @Put('config')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '微信小程序配置' })
  async set(@Body() data: SetDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: set

      return await this.weappConfigService.setWeappConfig(data);
    } catch (error) {
      throw new BusinessException('set操作失败', error);
    }
  }

  /**
   * 微信小程序配置
   * 路由: PUT domain
   * PHP路由: Route::put('domain', 'weapp.Config/setDomain')
   */
  @Put('domain')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '微信小程序配置' })
  async setDomain(@Body() data: SetDomainDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: setDomain

      return await this.weappConfigService.setDomain(data);
    } catch (error) {
      throw new BusinessException('setDomain操作失败', error);
    }
  }

  /**
   * 微信小程序配置
   * 路由: GET privacysetting
   * PHP路由: Route::get('privacysetting', 'weapp.Config/getPrivacySetting')
   */
  @Get('privacysetting')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '微信小程序配置' })
  async getPrivacySetting(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getPrivacySetting

      return await this.weappConfigService.getPrivacySetting();
    } catch (error) {
      throw new BusinessException('getPrivacySetting操作失败', error);
    }
  }

  /**
   * 微信小程序配置
   * 路由: PUT privacysetting
   * PHP路由: Route::put('privacysetting', 'weapp.Config/setPrivacySetting')
   */
  @Put('privacysetting')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '微信小程序配置' })
  async setPrivacySetting(
    @Body() data: SetPrivacySettingDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: setPrivacySetting

      return await this.weappConfigService.setPrivacySetting(data);
    } catch (error) {
      throw new BusinessException('setPrivacySetting操作失败', error);
    }
  }
}
